
function updateVedioState() {
    console.log("updateVedioState()");
    var index = localStorage["index"];
	var keyArr = index.split(",");
	var urlList = "";
	var showInfo = "";
	for(var i = 0; i < keyArr.length; i++) {
	    var info = localStorage[keyArr[i]];
		if(info == undefined || info == null) continue;
		var link = JSON.parse(info).link;
		if(urlList.length > 0) {
		    urlList = urlList + "," + link;
		}
		else {
		    urlList = link;
		}
	}
	console.log("Param URLList:" + urlList);
	var newIndex = '';
	$.ajax({
		url : "http://1.91testing.sinaapp.com/GetMultipleVedioInfo.php",
		type: "post",
		dataType: "json",
		data:{
			URLList: urlList
		},
		success:function(data, textStatus){
			var retLen = data.length;
			console.log("Param retLen:" + retLen);
			var hasNewerInfo = false;
			for(var i = 0; i < retLen; i++) {
				var obj = JSON.parse(data[i]);
				var currentLink = obj._url;
				var values = currentLink.split("/");
				var key = values[values.length - 2];
				var localInfo = localStorage[key];
				console.log("key :" + key + " -->");
				console.log("local  info:" + localInfo);
				console.log("server info:" + data[i]);
				if(localInfo == undefined || localInfo == null) continue;
				var localObj = JSON.parse(localInfo);
				
				var updateTime = obj._updateTime;
				var localUpdateTime = localObj.date;
				
				var state = obj._state;
				var localState = localObj.state;
				if(updateTime != localUpdateTime || state != localState) {
				    showInfo += obj._name + " : " + obj._state;
					createRecord(obj._name, obj._state, obj._updateTime, obj._url,obj._type);
					
					if(newIndex.length == 0)
						newIndex += localObj.key;
					else
						newIndex += ',' + localObj.key;
					
					hasNewerInfo = true;
				}
			}
			
			if(hasNewerInfo) {
				localStorage["newIndex"] = newIndex;
				var notification = webkitNotifications.createHTMLNotification(
					'../notification.html'  // html url - can be relative
				);
				notification.show();
			}
		}
	});
}
/*
// Conditionally initialize the options.
if (!localStorage.isInitialized) {
  localStorage.isActivated = true;   // The display activation.
  localStorage.frequency = 1;        // The display frequency, in minutes.
  localStorage.isInitialized = true; // The option initialization.
}
*/

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    console.log("onRequest: " + request.action);
    if (request.action == "getVideoIndex")
        sendResponse({videoIndex: localStorage['index']});
	
	else if(request.action == "createRecord") {
	    createRecord(request.name, request.state, request.updateTime, request.url,request.type);
		sendResponse({status: "done"});
	} 
	
	else if(request.action == "deleteRecord") {
	    deleteRecord(request.name);
		sendResponse({status: "done"});
	}
	
	else if(request.action == "storeStaticInfo") {
	    storeViedoStaticInfo(request.score, request.logo, request.lang, request.note, request.name);
		sendResponse({status: "done"});
	}
	
	else if(request.action == "deleteStaticInfo") {
	    deleteVideoStaticInfo(name);
		sendResponse({status: "done"});
	}
    
	else
      sendResponse({}); // snub them.
});

if(!localStorage.isInitialized) {
	localStorage.index = "";
    localStorage.isInitialized = true;
}

// Test for notification support.
if (window.webkitNotifications) {
  // While activated, show notifications at the display frequency.
  //if (JSON.parse(localStorage.isActivated)) { updateVedioState(); }
  updateVedioState();
  var interval = 0; // The display interval, in minutes.
  
  // check update every 30 minutes
  
  setInterval(function() {
    interval++;
    if(interval > 30) {
	    updateVedioState();
		interval = 0;		
	}
  }, 60000);
  
  
  
  
}
